//
//  UIScreen+TYFrame.m
//  TYUIKit
//
//  Created by TuyaInc on 2018/12/20.
//

#import "UIScreen+TYFrame.h"
#import "UIView+TYFrame.h"


///判断ipad
#define TYIsIpad ({\
BOOL isIpad = NO;\
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {\
    isIpad =  YES;\
}\
isIpad;\
})

#define NavBarHeight (TYIsIpad ? 50 : 44)

static CGFloat KTYScreenAdaptionBase = 375.0;

@interface UIScreen (TYFrame)

+ (BOOL)ty_isNotchScreen;

@end

@implementation UIScreen (TYFrame)

+ (BOOL)ty_isNotchScreen {
    if (@available(iOS 11.0, *)) {
        static BOOL isNotchScreen;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            if (![UIApplication sharedApplication].keyWindow) {
                UIWindow *win = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
                win.backgroundColor = [UIColor whiteColor];
                [UIApplication sharedApplication].delegate.window = win;
                [win makeKeyWindow];
            }
            
            isNotchScreen = [UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom > 0;
        });
        return isNotchScreen;
    } else {
        return NO;
    }
}

@end


#pragma mark - Public
inline BOOL TY_IsNotchScreen(void) {
    return [[UIDevice currentDevice].systemVersion floatValue] >= 11.0 ? [UIScreen ty_isNotchScreen] : NO;
}

inline CGRect TY_ScreenBounds(void) {
    return [UIScreen mainScreen].bounds;
}

inline CGFloat TY_ScreenWidth(void) {
    return [UIScreen mainScreen].bounds.size.width;
}

inline CGFloat TY_ScreenHeight(void) {
    return [UIScreen mainScreen].bounds.size.height;
}

inline UIEdgeInsets TY_ScreenSafeInsets(void) {
    if (@available(iOS 11.0, *)) {
        return [UIApplication sharedApplication].keyWindow.safeAreaInsets;
    } else {
        return UIEdgeInsetsZero;
    }
}

inline CGFloat TY_ScreenSafeTop(void) {
    return TY_ScreenSafeInsets().top;
}

inline CGFloat TY_ScreenSafeBottom(void) {
    return TY_ScreenSafeInsets().bottom;
}


inline CGFloat TY_StatusBarHeight(void) {
    
    return TY_IsNotchScreen() ? TY_ScreenSafeTop() : 20;
}

inline CGFloat TY_NaviBarHeight(void) {
    return TY_StatusBarHeight() + NavBarHeight;
}

inline CGFloat TY_TabBarHeight(void) {
    return 49 + TY_ScreenSafeBottom();
}



#pragma mark - ScreenAdaption

inline CGFloat TY_ScreenMultiplyCoefficient(void){
    return (TYIsIpad ? 0.6 : 1);
}


inline CGFloat TY_ScreenAdaptionLength(CGFloat length) {
    return TY_ScreenMultiplyCoefficient() * TY_PixelAlignedLength(length / KTYScreenAdaptionBase * MIN(TY_ScreenWidth(), TY_ScreenHeight()));
}
inline CGPoint TY_ScreenAdaptionPoint(CGPoint point) {
    return TY_PixelAlignedPoint(CGPointMake(TY_ScreenAdaptionLength(point.x),
                                            TY_ScreenAdaptionLength(point.y)));
}
inline CGSize TY_ScreenAdaptionSize(CGSize size) {
    return TY_PixelAlignedSize(CGSizeMake(TY_ScreenAdaptionLength(size.width),
                                          TY_ScreenAdaptionLength(size.height)));
}
inline CGRect TY_ScreenAdaptionRect(CGRect rect) {
    return TY_PixelAlignedRect(CGRectMake(TY_ScreenAdaptionLength(rect.origin.x),
                                          TY_ScreenAdaptionLength(rect.origin.y),
                                          TY_ScreenAdaptionLength(rect.size.width),
                                          TY_ScreenAdaptionLength(rect.size.height)));
}
